import { htmlTextReg, urlReg } from './reg'
import { get } from 'lodash'
import moment from 'moment'
import store from '@/store'
import { noMenuIdList } from './menuConfig'

export function getInnerText(html) {
  return html.replace(htmlTextReg, '')
}

export function getUrl(str) {
  const value = str.match(urlReg)
  if (value && value.length) {
    return value[0]
  }

  return null
}

export function getResourceNum(data = {}) {
  let num = 0
  for (let key in data) {
    if (key !== 'dimension1' && key !== 'icon') {
      num += data[key]
    }
  }
  return num
}

export function parseJSON(json) {
  try {
    return JSON.parse(json)
  } catch (error) {
    return undefined
  }
}

export function getUpnName(upn) {
  if (upn && upn.indexOf('@') > -1) {
    const [name] = upn.split('@')
    const [first, last] = name.split('.')
    return { first, last, name }
  }
  return {}
}

export function getMenuId(route) {
  const { query, path } = route
  if (path.indexOf('bot-') !== -1) {
    return get(query, 'botId')
  }
  if (
    path.indexOf('dashboard-') !== -1 ||
    path.indexOf('report-') !== -1 ||
    path.indexOf('dataset-') !== -1 ||
    path.indexOf('scorecard-') !== -1
  ) {
    return get(query, 'pageid')
  }
  if (path.indexOf('form-') !== -1) {
    return get(query, 'formCode')
  }
  if (
    path.indexOf('NextTicketPriorityRules-') !== -1 ||
    path.indexOf('UserFilterRule-') !== -1 ||
    path.indexOf('ClientWatchList-') !== -1
  ) {
    return get(query, 'name')
  }
  if(path&&noMenuIdList.includes(path)){
    return path
  }
  if(path === '/home') {
    return 'home'
  }

  return get(route, 'meta.menuId') || get(query, 'menuId')
}

export function getLayout(route) {
  if (route.name === 'mspbots-reports-scorecard-report-index') {
    return 'Scorecard'
  }

  if (route.name === 'mspbots-reports-index') {
    return 'Report'
  }
}

export function queryURLParams(url = '') {
  const slpitParams = url.split('&') || []
  let str = []

  slpitParams.forEach((query, index) => {
    if (query.indexOf('=') !== -1) {
      str.push(query)
    } else if (index > 0) {
      str[index - 1] += `%26${query}`
    }
  })

  return str.join('&')
}

export function queryToObject(search = '') {
  const url = decodeURIComponent(search)
  const res = {}
  const paramArr = new URLSearchParams(url)
  paramArr.forEach((val, key) => {
    res[key] = val
  })
  return res
}

export function query(key, search = '',isDecode = true) {
  const url = isDecode ? decodeURIComponent(search) : search
  const res = new URLSearchParams(url)
  return res.get(key)
}

export function timeFormat(value, timeToken, dateToken, monday) {
  if (value) {
    const date = dateToken || store.state.user.timeFormat || 'MM/DD/YYYY'
    const format = `${date} ${timeToken}`
    const timezoneId = store.state.user.timezoneId

    return monday
      ? `${moment.tz(value, timezoneId).add(6, 'd').format(format)} ${moment
          .tz(timezoneId)
          .format('zz')}`
      : `${moment.tz(value, timezoneId).format(format)} ${moment
          .tz(timezoneId)
          .format('zz')}`
  }

  return value
}

export function hasPermission(key) {
  return window.SITE_CONFIG['permissions'].indexOf(key) !== -1 || false
}

export function unionIntegrationList(list, key) {
  let tempList = []
  if (hasPermission('sys:tenant:dataconnectors:cw')) {
    tempList.push('CW')
  }
  if (hasPermission('sys:tenant:dataconnectors:td')) {
    tempList.push('TD')
  }
  if (hasPermission('sys:tenant:dataconnectors:tsheet')) {
    tempList.push('TSHEET')
  }
  if (hasPermission('sys:tenant:dataconnectors:teams')) {
    tempList.push('TEAMS')
  }
  if (hasPermission('sys:tenant:dataconnectors:quickbooks')) {
    tempList.push('QUICKBOOKS')
  }
  if (hasPermission('sys:tenant:dataconnectors:quickbooksonline')) {
    tempList.push('QUICKBOOKSONLINE')
  }
  if (hasPermission('sys:tenant:dataconnectors:autotask')) {
    tempList.push('AUTOTASK')
  }
  if (hasPermission('sys:tenant:dataconnectors:science')) {
    tempList.push('SCIENCE')
  }
  if (hasPermission('sys:tenant:dataconnectors:jira')) {
    tempList.push('JIRA')
  }
  if (hasPermission('sys:tenant:dataconnectors:hubspot')) {
    tempList.push('HUBSPOT')
  }
  if (hasPermission('sys:tenant:dataconnectors:sqlagent')) {
    tempList.push('SQLAGENT')
  }
  if (hasPermission('sys:tenant:dataconnectors:automateagent')) {
    tempList.push('AUTOMATEAGENT')
  }
  if (hasPermission('sys:tenant:dataconnectors:kvsaonpremise')) {
    tempList.push('KVSAONPREMISE')
  }
  if (hasPermission('sys:tenant:dataconnectors:cwmonpremise')) {
    tempList.push('CWMONPREMISE')
  }
  if (hasPermission('sys:tenant:dataconnectors:cwautoonpremise')) {
    tempList.push('CWAUTOONPREMISE')
  }

  if (hasPermission('sys:tenant:dataconnectors:smileback')) {
    tempList.push('SMILEBACK')
  }
  if (hasPermission('sys:tenant:dataconnectors:liongard')) {
    tempList.push('LIONGARD')
  }
  if (hasPermission('sys:tenant:dataconnectors:xero')) {
    tempList.push('XERO')
  }
  if (hasPermission('sys:tenant:dataconnectors:ringcentral')) {
    tempList.push('RINGCENTRAL')
  }
  if (hasPermission('sys:tenant:dataconnectors:automate')) {
    tempList.push('AUTOMATE')
  }
  if (hasPermission('sys:tenant:dataconnectors:datto')) {
    tempList.push('DATTO')
  }
  if (hasPermission('sys:tenant:dataconnectors:dropsuite')) {
    tempList.push('DROPSUITE')
  }
  if (hasPermission('sys:tenant:dataconnectors:ninjaone')) {
    tempList.push('NINJAONE')
  }
  if (hasPermission('sys:tenant:dataconnectors:solarwind')) {
    tempList.push('SOLARWIND')
  }
  if (hasPermission('sys:tenant:dataconnectors:veeam')) {
    tempList.push('VEEAM')
  }
  if (hasPermission('sys:tenant:dataconnectors:ameritrade')) {
    tempList.push('AMERITRADE')
  }
  if (hasPermission('sys:tenant:dataconnectors:solarwindcentral')) {
    tempList.push('SOLARWINDCENTRAL')
  }
  if (hasPermission('sys:tenant:dataconnectors:continuum')) {
    tempList.push('CONTINUUM')
  }
  if (hasPermission('sys:tenant:dataconnectors:dynu')) {
    tempList.push('DYNU')
  }
  if (hasPermission('sys:tenant:dataconnectors:auvik')) {
    tempList.push('AUVIK')
  }
  if (hasPermission('sys:tenant:dataconnectors:keap')) {
    tempList.push('KEAP')
  }
  if (hasPermission('sys:tenant:dataconnectors:crewhu')) {
    tempList.push('CREWHU')
  }
  if (hasPermission('sys:tenant:dataconnectors:kaseyabms')) {
    tempList.push('KASEYABMS')
  }
  if (hasPermission('sys:tenant:dataconnectors:chargebee')) {
    tempList.push('CHARGEBEE')
  }
  if (hasPermission('sys:tenant:dataconnectors:dropbox')) {
    tempList.push('DROPBOX')
  }
  if (hasPermission('sys:tenant:dataconnectors:thermometer')) {
    tempList.push('THERMOMETER')
  }
  if (hasPermission('sys:tenant:dataconnectors:itglue')) {
    tempList.push('ITGLUE')
  }
  if (hasPermission('sys:tenant:dataconnectors:simplesat')) {
    tempList.push('SIMPLESAT')
  }
  if (hasPermission('sys:tenant:dataconnectors:prtg')) {
    tempList.push('PRTG')
  }
  if (hasPermission('sys:tenant:dataconnectors:veeamconsole')) {
    tempList.push('VEEAMCONSOLE')
  }
  if (hasPermission('sys:tenant:dataconnectors:sophos')) {
    tempList.push('SOPHOS')
  }
  if (hasPermission('sys:tenant:dataconnectors:kaseyavsa')) {
    tempList.push('KASEYAVSA')
  }
  if (hasPermission('sys:tenant:dataconnectors:dattobcdr')) {
    tempList.push('DATTOBCDR')
  }
  if (hasPermission('sys:tenant:dataconnectors:sassalert')) {
    tempList.push('SAASALERT')
  }
  if (hasPermission('sys:tenant:dataconnectors:backupradar')) {
    tempList.push('BACKUPRADAR')
  }
  if (hasPermission('sys:tenant:dataconnectors:acronis')) {
    tempList.push('ACRONIS')
  }
  if (hasPermission('sys:tenant:dataconnectors:halo')) {
    tempList.push('HALO')
  }
  if (hasPermission('sys:tenant:dataconnectors:bizratings')) {
    tempList.push('BIZRATINGS')
  }
  if (hasPermission('sys:tenant:dataconnectors:syncromsp')) {
    tempList.push('SYNCROMSP')
  }
  if (hasPermission('sys:tenant:dataconnectors:addigy')) {
    tempList.push('ADDIGY')
  }
  if (hasPermission('sys:tenant:dataconnectors:bvoip')) {
    tempList.push('BVOIP')
  }
  if (hasPermission('sys:tenant:dataconnectors:webroot')) {
    tempList.push('WEBROOT')
  }
  if (hasPermission('sys:tenant:dataconnectors:ns')) {
    tempList.push('NS')
  }
  if (hasPermission('sys:tenant:dataconnectors:huntress')) {
    tempList.push('HUNTRESS')
  }
  if (hasPermission('sys:tenant:dataconnectors:cloudradial')) {
    tempList.push('CLOUDRADIAL')
  }
  if (hasPermission('sys:tenant:dataconnectors:logicmonitor')) {
    tempList.push('LOGICMONITOR')
  }
  if (hasPermission('sys:tenant:dataconnectors:opsgenie')) {
    tempList.push('OPSGENIE')
  }
  if (hasPermission('sys:tenant:dataconnectors:bamboohr')) {
    tempList.push('BAMBOOHR')
  }
  if (hasPermission('sys:tenant:dataconnectors:ciscomeraki')) {
    tempList.push('CISCOMERAKI')
  }
  if (hasPermission('sys:tenant:dataconnectors:onedrive')) {
    tempList.push('ONEDRIVE')
  }
  if (hasPermission('sys:tenant:dataconnectors:ciscoumbrella')) {
    tempList.push('CISCOUMBRELLA')
  }
  if (hasPermission('sys:tenant:dataconnectors:openai')) {
    tempList.push('OPENAI')
  }
  if (hasPermission('sys:tenant:dataconnectors:fieldeffect')) {
    tempList.push('FIELDEFFECT')
  }
  if (hasPermission('sys:tenant:dataconnectors:logmein')) {
    tempList.push('LOGMEIN')
  }
  if (hasPermission('sys:tenant:dataconnectors:freshdesk')) {
    tempList.push('FRESHDESK')
  }
  if (hasPermission('sys:tenant:dataconnectors:nablepassportal')) {
    tempList.push('NABLEPASSPORTAL')
  }
  if (hasPermission('sys:tenant:dataconnectors:sentinelone')) {
    tempList.push('SENTINELONE')
  }
  if (hasPermission('sys:tenant:dataconnectors:slack')) {
    tempList.push('SLACK')
  }
  if (hasPermission('sys:tenant:dataconnectors:veeambackup')) {
    tempList.push('VEEAMBACKUP365')
  }
  if (hasPermission('sys:tenant:dataconnectors:simpleinout')) {
    tempList.push('SIMPLEINOUT')
  }
  tempList.push('UNCATEGORIZED')
  return list.filter(val => tempList.indexOf(get(val, key)) > -1)
}
